<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<script language="javascript">

	var u = navigator.userAgent,app = navigator.appVersion;
	
	var temp = false;
	//if(u.indexOf('Android') >= 0) {
	//	temp = true;
	//}

    var callBackDataType;
    var callBackMethod;
    var callBackData;

    function _loadAjaxParams(url, dataType, params, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        $.ajax( {
            async :temp,
            cache :false,
            url :url,
            data :getParams(params),
            type :"post",
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            timeout :60000,
            beforeSend : function(XMLHttpRequest) {
                showLoading();
            },
            success : function(data, textStatus) {
                showResponse(data);
            },
            complete : function(XMLHttpRequest, textStatus) {
                hideLoading();
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
            	if(flag) {
                	reportError(textStatus, errorThrown);
            	} else {
            		if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
            			_loadAjaxParams(url, dataType, params, method, true);
                    } else {
                        reportError(textStatus, errorThrown);
                    }
            	}
            }
        });
    }
    
    function _loadAjaxParamsNoLoad(url, dataType, params, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        $.ajax( {
            async :temp,
            cache :false,
            url :url,
            data :getParams(params),
            type :"post",
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            success : function(data, textStatus) {
                showResponse(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
            	if(flag) {
            		reportError(textStatus, errorThrown);
                } else {
                	if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
                		_loadAjaxParamsNoLoad(url, dataType, params, method, true);
                    } else {
                        reportError(textStatus, errorThrown);
                    }
                }
            }
        });
    }

    function _loadAjaxForm(url, dataType, formid, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        if (!formid) {
            alert("Formid is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        var params = $("#" + formid).serialize();
        $.ajax( {
            async :temp,
            cache :false,
            url :url,
            data :getParams(params),
            type :"post",
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            timeout :60000,
            beforeSend : function(XMLHttpRequest) {
                showLoading();
            },
            success : function(data, textStatus, jqXHR) {
                showResponse(data);
            },
            complete : function(XMLHttpRequest, textStatus) {
                hideLoading();
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                if(flag) {
                	reportError(textStatus, errorThrown);
                } else {
                	if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
                		_loadAjaxForm(url, dataType, formid, method, true);
                    } else {
                        reportError(textStatus, errorThrown);
                    }
                }
            }
        });
    }
    
    function _loadAjaxNoLoad(url, dataType, params, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        $.ajax( {
            async :temp,
            cache :false,
            url :url,
            data :getParams(params),
            type :"post",
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            timeout :60000,
            success : function(data, textStatus) {
                showResponse(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                if(flag) {
                	reportError(textStatus, errorThrown);
            	} else {
            		if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
            			_loadAjaxNoLoad(url, dataType, params, method, true);
            		} else {
            			reportError(textStatus, errorThrown);
            		}
            	}
            }
        });
    }
    
    function _loadAjaxFormNoLoad(url, dataType, formid, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        if (!formid) {
            alert("Formid is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        var params = $("#" + formid).serialize();
        $.ajax( {
            async :temp,
            cache :false,
            url :url,
            data :getParams(params),
            type :"post",
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            success : function(data, textStatus, jqXHR) {
                showResponse(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                if(flag) {
                    reportError(textStatus, errorThrown);
                } else {
                	if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
                		_loadAjaxFormNoLoad(url, dataType, formid, method, true);
                    } else {
                        reportError(textStatus, errorThrown);
                    }
                }
            }
        });
    }
    
    function _ajaxFileUpload(url, dataType, fileid, formid, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        if (!fileid) {
            alert("Fileid is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        var params = "";
        if (formid) {
            params = $("#" + formid).serializeArray();
        }
        $.ajaxFileUpload({
            async :temp,
            cache :false,
            secureuri :false,
            url :url,
            data :getJson(params),
            fileElementId :fileid,
            dataType :dataType,
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            timeout :60000,
            beforeSend : function(XMLHttpRequest) {
                showLoading();
            },
            success : function(data, textStatus, jqXHR) {
                showResponse(data);
            },
            complete : function(XMLHttpRequest, textStatus) {
                hideLoading();
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                if(flag) {
                    reportError(textStatus, errorThrown);
                } else {
                	if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
                		_ajaxFileUpload(url, dataType, fileid, formid, method, true);
                    } else {
                        reportError(textStatus, errorThrown);
                    }
                }
            }
        });
    }
    
    function _ajaxFileUploadNoLoad(url, dataType, fileid, formid, method, flag) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!dataType) {
            alert("DataType is not valid!");
            return;
        }
        if (!fileid) {
            alert("Fileid is not valid!");
            return;
        }
        callBackDataType = dataType;
        callBackMethod = method;
        var params = "";
        if (formid) {
            params = $("#" + formid).serializeArray();
        }
        $.ajaxFileUpload({
            async :temp,
            cache :false,
            secureuri :false,
            url :url,
            data :getJson(params),
            fileElementId :fileid,
            dataType :dataType,
            contentType :"application/x-www-form-urlencoded; charset=utf-8",
            timeout :60000,

            success : function(data, textStatus, jqXHR) {
                showResponse(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
            	if(flag) {
                    reportError(textStatus, errorThrown);
                } else {
                	if (XMLHttpRequest.readyState == 0 && textStatus == "error") {
                		_ajaxFileUploadNoLoad(url, dataType, fileid, formid, method, true);
                    } else {
                        reportError(textStatus, errorThrown);
                    }
                }
            }
        });
    }
    
    function _loadAjaxTree(url, treeid, checkFlag, method1, autoParam, method2) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!treeid) {
            alert("Treeid is not valid!");
            return;
        }
        if (!checkFlag) {
            checkFlag = false;
        }
        var setting = {
            async :{
                enable :true,
                type :"post",
                contentType :"application/x-www-form-urlencoded; charset=utf-8",
                url :getUrl(url),
                dataFilter :function(treeId, parentNode, responseData) {
                    if (responseData.resultList != null) {
                        return responseData.resultList;
                    }
                    return null;
                },
                autoParam: [autoParam]
            },
            view: {
                showLine: false,
                showIcon: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            }, 
            callback: {
                onClick: method1,
                onAsyncSuccess: method2,
                onCheck:method1
            },
            check: {
                enable: checkFlag,
                autoCheckTrigger: true,
                chkStyle: "checkbox"
                
            }
        };
        
        var treeObj = $.fn.zTree.init($("#" + treeid), setting);
        
        return treeObj;
    }
    
    function _loadCheckBoxAjaxTree(url, treeid, checkFlag, method1, autoParam, method2) {
        if (!url) {
            alert("Url is not valid!");
            return;
        }
        if (!treeid) {
            alert("Treeid is not valid!");
            return;
        }
        if (!checkFlag) {
            checkFlag = false;
        }
        var setting = {
            async :{
                enable :true,
                type :"post",
                contentType :"application/x-www-form-urlencoded; charset=utf-8",
                url :getUrl(url),
                dataFilter :function(treeId, parentNode, responseData) {
                    if (responseData.resultList != null) {
                        return responseData.resultList;
                    }
                    return null;
                },
                autoParam: [autoParam]
            },
            view: {
                showLine: false,
                showIcon: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            }, 
            callback: {
                onClick: method1,
                onAsyncSuccess: method2,
                onCheck:method1
            },
            check: {
                enable: checkFlag,
                autoCheckTrigger: true,
                chkStyle: "checkbox",
                chkboxType: { "Y" : "", "N" : "" }
            }
        };
        
        var treeObj = $.fn.zTree.init($("#" + treeid), setting);
        
        return treeObj;
    }
    
    function showResponse(data) {
        if (callBackMethod) {
            callBackData = data;
            if (typeof callBackMethod == "string") {
                if (callBackMethod.indexOf("(") >= 0) {
                    eval(callBackMethod);
                } else {
                    eval(callBackMethod + "()");
                }
            } else {
                callBackMethod.call();
            }
        }
    }

    function _callBackObj(id) {
        var err = document.createElement("span");
        err.innerHTML = callBackData;
        if ($(err).find("#errorCode").get(0)) {
            var errorCode = $(err).find("#errorCode").get(0).value;
            if (errorCode == 0) {
                var errorMsg = $(err).find("#errorMsg").get(0).value;
                alert(errorMsg);
            }
            if (errorCode == 1) {
                alert("<s:text name='timeout_info' />");
                WeixinJSBridge.invoke('closeWindow',{},function(res){});
            }
            if (errorCode == 2) {
                alert("<s:text name='outPermission_info' />");
            }
            hideLoading();
        } else {
            var obj = null;
            if (callBackDataType == "html") {
                if (JSON.stringify(callBackData).match("^\{(.+:.+,*){1,}\}$")) {
                    obj = callBackData;
                } else {
                    var dv = document.createElement("span");
                    dv.innerHTML = callBackData;
                    if (!id) {
                        obj = dv;
                    } else {
                        obj = $(dv).find("#" + id).get(0);
                    }
                }
            } else if (callBackDataType == "json") {
                obj = callBackData;
            }
            if (!obj) {
                alert("Obj is not valid!");
                hideLoading();
            }
            return obj;
        }
    }
    
    function showLoading() {
        $.messager.progress({
            msg:"Loading data..."
        });
    }

    function hideLoading() {
        setTimeout(function(){
            $(".table_list").find("tr:odd").addClass("odd");
            $(".table_list").find("tr").hover(function(){
                $(this).addClass("td_over");
            },function(){
                $(this).removeClass("td_over");
            });
            $.messager.progress("close");
        }, 500);
    }

    function reportError(textStatus, errorThrown) {
    	/* alert("您的网络不稳定，请稍后重试！:" + errorThrown, null, function(){ */
        alert("您的网络不稳定，请稍后重试！", null, function(){
            hideLoading();
        });
    }
</script>